package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.entity.Good;
import com.zcx.qshop.view.CommodityAssessStartView;
import com.zcx.qshop.view.StartView;

import java.util.List;

/**
 * Created by Administrator on 2015/9/3 0003.
 */
public class CommodityAssessAdapter extends QSAdapter<Good> {

    private LayoutInflater layoutInflater;

    public CommodityAssessAdapter(Context context, List<Good> objects) {

        super(context, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Good good = getItem(position);

        ViewHolder viewHolder;

        convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_commodity_assess, null)));

        viewHolder.img.setImageURI(Uri.parse(good.picurl));

        viewHolder.name.setText(good.title);

        viewHolder.price.setText("￥" + good.price);

        viewHolder.unit.setText("元/" + good.unit);

        viewHolder.content.setText(good.content);

        viewHolder.start.setOnItemSelectListener(new StartView.OnItemSelectListener() {

            @Override
            public void onItemSelect(int index) {

                good.start = index + "";

            }

        });

        viewHolder.content.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                good.content = s.toString();

            }

        });

        return convertView;

    }

    private class ViewHolder{

        @BoundView(R.id.commodity_assess_img) public SimpleDraweeView img;

        @BoundView(R.id.commodity_assess_name) public TextView name;

        @BoundView(R.id.commodity_assess_price) public TextView price;

        @BoundView(R.id.commodity_assess_unit) public TextView unit;

        @BoundView(R.id.commodity_assess_content) private EditText content;

        @BoundView(R.id.commodity_assess_start) private CommodityAssessStartView start;

    }

}
